/**
* @file 单轴参数设置
* @brief
*
*
*
* @author
* @date
* @see
* @note
*
*
*
*/
#include "axispidwidget.h"

QAxisPIDWidget::QAxisPIDWidget(int i32AxisNo,QString sAxisName,QWidget *parent) : QWidget(parent)
{
    _i32AxisNo = i32AxisNo;
    _sAxisName = sAxisName;

    this->setStyleSheet("background-color:transparent;QLabel{color:rgb(200,200,200);}");

    InitUI();

    this->setMinimumHeight(400);

//    connect(_pSetParamButton,&QPushButton::clicked,this,&QAxisPIDWidget::onSetParamButtonClick);

}

QAxisPIDWidget::~QAxisPIDWidget()
{

}
/**
* @brief 初始化界面
* @note
*
*/
void QAxisPIDWidget::InitUI()
{
    //参数设置
    _pParamSetWidget = new QTableWidget(this);
    QString sTableStyle = "QTableWidget{border:0px solid;border-top:1px solid rgb(150, 150, 150);";
    sTableStyle = sTableStyle + "font-size:16px;font-family: 'Microsoft YaHei';color:rgb(255,255,255);}";
    sTableStyle = sTableStyle + "QTableWidget::Item{border:0px solid rgb(150, 150, 150);border-bottom:1px solid rgb(150, 150, 150);}";
    _pParamSetWidget->setStyleSheet(sTableStyle);
    _pParamSetWidget->setShowGrid(false);
//    _pParamSetWidget->setStyleSheet("border:0px;font-size:16px;font-family: 'Microsoft YaHei';");
    _pParamSetWidget->horizontalHeader()->setStretchLastSection(true);//最后一列充满
    _pParamSetWidget->verticalHeader()->hide();
    _pParamSetWidget->setColumnCount(5);
    _pParamSetWidget->setRowCount(18);

    _pParamSetWidget->setColumnWidth(0,80);
    _pParamSetWidget->setColumnWidth(1,80);
    _pParamSetWidget->setColumnWidth(2,80);
    _pParamSetWidget->setColumnWidth(3,80);

    QStringList sheader;
    sheader<<"环类型"<<"参数"<<"设置值"<<"反馈值"<<"操作";
    _pParamSetWidget->setHorizontalHeaderLabels(sheader);

    int nTableRow = 0;
    QStringList sNamelist;
    sNamelist<<"比例系数"<<"积分系数"<<"微分系数";
    _pParamSetWidget->setItem(nTableRow,0,new QTableWidgetItem("注入电流环"));
    _pParamSetWidget->item(nTableRow,0)->setFlags(Qt::ItemIsEnabled);
    _pParamSetWidget->setSpan(nTableRow,0,sNamelist.size(),1);

    QPushButton* pbutton = new QPushButton("设置",this);
    pbutton->setStyleSheet(
                "QPushButton{padding:5px;border-radius:5px;color:rgb(255,255,255);font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(46,97,186);}"
                "QPushButton:hover{padding:5px;border-radius:5px;color:white;font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(96,131,203);}"
                "QPushButton:pressed{padding:5px;border-radius:5px;color:white;font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(139,165,217);}"
                "QPushButton:disabled { background-color: rgb(200,200,200);color:black;}"
                                );
    pbutton->setObjectName(QString::number(0x5A));
    connect(pbutton,&QPushButton::clicked,this,&QAxisPIDWidget::onSetParamButtonClick);
    _pParamSetWidget->setCellWidget(nTableRow,4,pbutton);
    _pParamSetWidget->setSpan(nTableRow,4,sNamelist.size(),1);

    for(int i = nTableRow; i < nTableRow+sNamelist.size(); i++)
    {
        _pParamSetWidget->setItem(i,1,new QTableWidgetItem(sNamelist[i-nTableRow]));
        _pParamSetWidget->setItem(i,2,new QTableWidgetItem("0"));
        _pParamSetWidget->setItem(i,3,new QTableWidgetItem("0"));

        _pParamSetWidget->item(i,1)->setFlags(Qt::ItemIsEnabled);
        _pParamSetWidget->item(i,3)->setFlags(Qt::ItemIsEnabled);
        _pParamSetWidget->item(i,2)->setTextAlignment(Qt::AlignCenter);
        _pParamSetWidget->item(i,3)->setTextAlignment(Qt::AlignCenter);
    }

    nTableRow = nTableRow + sNamelist.size();

    sNamelist.clear();
    sNamelist<<"比例系数"<<"积分系数"<<"微分系数";
    _pParamSetWidget->setItem(nTableRow,0,new QTableWidgetItem("注入速度环"));
    _pParamSetWidget->item(nTableRow,0)->setFlags(Qt::ItemIsEnabled);
    _pParamSetWidget->setSpan(nTableRow,0,sNamelist.size(),1);

    pbutton = new QPushButton("设置",this);
    pbutton->setStyleSheet(
                "QPushButton{padding:5px;border-radius:5px;color:rgb(255,255,255);font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(46,97,186);}"
                "QPushButton:hover{padding:5px;border-radius:5px;color:white;font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(96,131,203);}"
                "QPushButton:pressed{padding:5px;border-radius:5px;color:white;font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(139,165,217);}"
                "QPushButton:disabled { background-color: rgb(200,200,200);color:black;}"
                                );
    pbutton->setObjectName(QString::number(0x45));
    connect(pbutton,&QPushButton::clicked,this,&QAxisPIDWidget::onSetParamButtonClick);
    _pParamSetWidget->setCellWidget(nTableRow,4,pbutton);
    _pParamSetWidget->setSpan(nTableRow,4,sNamelist.size(),1);

    for(int i = nTableRow; i < nTableRow+sNamelist.size(); i++)
    {
        _pParamSetWidget->setItem(i,1,new QTableWidgetItem(sNamelist[i-nTableRow]));
        _pParamSetWidget->setItem(i,2,new QTableWidgetItem("0"));
        _pParamSetWidget->setItem(i,3,new QTableWidgetItem("0"));
        _pParamSetWidget->item(i,1)->setFlags(Qt::ItemIsEnabled);
        _pParamSetWidget->item(i,3)->setFlags(Qt::ItemIsEnabled);
        _pParamSetWidget->item(i,2)->setTextAlignment(Qt::AlignCenter);
        _pParamSetWidget->item(i,3)->setTextAlignment(Qt::AlignCenter);
    }

    nTableRow = nTableRow + sNamelist.size();

    sNamelist.clear();
    sNamelist<<"比例系数"<<"积分系数"<<"微分系数";
    _pParamSetWidget->setItem(nTableRow,0,new QTableWidgetItem("注入位置环"));
    _pParamSetWidget->item(nTableRow,0)->setFlags(Qt::ItemIsEnabled);
    _pParamSetWidget->setSpan(nTableRow,0,sNamelist.size(),1);

    pbutton = new QPushButton("设置",this);
    pbutton->setStyleSheet(
                "QPushButton{padding:5px;border-radius:5px;color:rgb(255,255,255);font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(46,97,186);}"
                "QPushButton:hover{padding:5px;border-radius:5px;color:white;font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(96,131,203);}"
                "QPushButton:pressed{padding:5px;border-radius:5px;color:white;font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(139,165,217);}"
                "QPushButton:disabled { background-color: rgb(200,200,200);color:black;}"
                                );
    pbutton->setObjectName(QString::number(0x4A));
    connect(pbutton,&QPushButton::clicked,this,&QAxisPIDWidget::onSetParamButtonClick);
    _pParamSetWidget->setCellWidget(nTableRow,4,pbutton);
    _pParamSetWidget->setSpan(nTableRow,4,sNamelist.size(),1);

    for(int i = nTableRow; i < nTableRow+sNamelist.size(); i++)
    {
        _pParamSetWidget->setItem(i,1,new QTableWidgetItem(sNamelist[i-nTableRow]));
        _pParamSetWidget->setItem(i,2,new QTableWidgetItem("0"));
        _pParamSetWidget->setItem(i,3,new QTableWidgetItem("0"));
        _pParamSetWidget->item(i,1)->setFlags(Qt::ItemIsEnabled);
        _pParamSetWidget->item(i,3)->setFlags(Qt::ItemIsEnabled);
        _pParamSetWidget->item(i,2)->setTextAlignment(Qt::AlignCenter);
        _pParamSetWidget->item(i,3)->setTextAlignment(Qt::AlignCenter);
    }

    nTableRow = nTableRow + sNamelist.size();

    sNamelist.clear();
    sNamelist<<"比例系数"<<"积分系数"<<"微分系数";
    _pParamSetWidget->setItem(nTableRow,0,new QTableWidgetItem("调试位置环"));
    _pParamSetWidget->item(nTableRow,0)->setFlags(Qt::ItemIsEnabled);
    _pParamSetWidget->setSpan(nTableRow,0,sNamelist.size(),1);

    pbutton = new QPushButton("设置",this);
    pbutton->setStyleSheet(
                "QPushButton{padding:5px;border-radius:5px;color:rgb(255,255,255);font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(46,97,186);}"
                "QPushButton:hover{padding:5px;border-radius:5px;color:white;font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(96,131,203);}"
                "QPushButton:pressed{padding:5px;border-radius:5px;color:white;font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(139,165,217);}"
                "QPushButton:disabled { background-color: rgb(200,200,200);color:black;}"
                                );
    pbutton->setObjectName(QString::number(0x2A));
    connect(pbutton,&QPushButton::clicked,this,&QAxisPIDWidget::onSetParamButtonClick);
    _pParamSetWidget->setCellWidget(nTableRow,4,pbutton);
    _pParamSetWidget->setSpan(nTableRow,4,sNamelist.size(),1);

    for(int i = nTableRow; i < nTableRow+sNamelist.size(); i++)
    {
        _pParamSetWidget->setItem(i,1,new QTableWidgetItem(sNamelist[i-nTableRow]));
        _pParamSetWidget->setItem(i,2,new QTableWidgetItem("0"));
        _pParamSetWidget->setItem(i,3,new QTableWidgetItem("0"));
        _pParamSetWidget->item(i,1)->setFlags(Qt::ItemIsEnabled);
        _pParamSetWidget->item(i,3)->setFlags(Qt::ItemIsEnabled);
        _pParamSetWidget->item(i,2)->setTextAlignment(Qt::AlignCenter);
        _pParamSetWidget->item(i,3)->setTextAlignment(Qt::AlignCenter);
    }

    nTableRow = nTableRow + sNamelist.size();

    sNamelist.clear();
    sNamelist<<"比例系数"<<"积分系数"<<"微分系数";
    _pParamSetWidget->setItem(nTableRow,0,new QTableWidgetItem("调试速度环"));
    _pParamSetWidget->item(nTableRow,0)->setFlags(Qt::ItemIsEnabled);
    _pParamSetWidget->setSpan(nTableRow,0,sNamelist.size(),1);

    pbutton = new QPushButton("设置",this);
    pbutton->setStyleSheet(
                "QPushButton{padding:5px;border-radius:5px;color:rgb(255,255,255);font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(46,97,186);}"
                "QPushButton:hover{padding:5px;border-radius:5px;color:white;font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(96,131,203);}"
                "QPushButton:pressed{padding:5px;border-radius:5px;color:white;font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(139,165,217);}"
                "QPushButton:disabled { background-color: rgb(200,200,200);color:black;}"
                                );
    pbutton->setObjectName(QString::number(0x25));
    connect(pbutton,&QPushButton::clicked,this,&QAxisPIDWidget::onSetParamButtonClick);
    _pParamSetWidget->setCellWidget(nTableRow,4,pbutton);
    _pParamSetWidget->setSpan(nTableRow,4,sNamelist.size(),1);

    for(int i = nTableRow; i < nTableRow+sNamelist.size(); i++)
    {
        _pParamSetWidget->setItem(i,1,new QTableWidgetItem(sNamelist[i-nTableRow]));
        _pParamSetWidget->setItem(i,2,new QTableWidgetItem("0"));
        _pParamSetWidget->setItem(i,3,new QTableWidgetItem("0"));
        _pParamSetWidget->item(i,1)->setFlags(Qt::ItemIsEnabled);
        _pParamSetWidget->item(i,3)->setFlags(Qt::ItemIsEnabled);
        _pParamSetWidget->item(i,2)->setTextAlignment(Qt::AlignCenter);
        _pParamSetWidget->item(i,3)->setTextAlignment(Qt::AlignCenter);
    }

    nTableRow = nTableRow + sNamelist.size();

    sNamelist.clear();
    sNamelist<<"比例系数"<<"积分系数"<<"微分系数";
    _pParamSetWidget->setItem(nTableRow,0,new QTableWidgetItem("调试电流环"));
    _pParamSetWidget->item(nTableRow,0)->setFlags(Qt::ItemIsEnabled);
    _pParamSetWidget->setSpan(nTableRow,0,sNamelist.size(),1);

    pbutton = new QPushButton("设置",this);
    pbutton->setStyleSheet(
                "QPushButton{padding:5px;border-radius:5px;color:rgb(255,255,255);font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(46,97,186);}"
                "QPushButton:hover{padding:5px;border-radius:5px;color:white;font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(96,131,203);}"
                "QPushButton:pressed{padding:5px;border-radius:5px;color:white;font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(139,165,217);}"
                "QPushButton:disabled { background-color: rgb(200,200,200);color:black;}"
                                );
    pbutton->setObjectName(QString::number(0x3A));
    connect(pbutton,&QPushButton::clicked,this,&QAxisPIDWidget::onSetParamButtonClick);
    _pParamSetWidget->setCellWidget(nTableRow,4,pbutton);
    _pParamSetWidget->setSpan(nTableRow,4,sNamelist.size(),1);

    for(int i = nTableRow; i < nTableRow+sNamelist.size(); i++)
    {
        _pParamSetWidget->setItem(i,1,new QTableWidgetItem(sNamelist[i-nTableRow]));
        _pParamSetWidget->setItem(i,2,new QTableWidgetItem("0"));
        _pParamSetWidget->setItem(i,3,new QTableWidgetItem("0"));
        _pParamSetWidget->item(i,1)->setFlags(Qt::ItemIsEnabled);
        _pParamSetWidget->item(i,3)->setFlags(Qt::ItemIsEnabled);
        _pParamSetWidget->item(i,2)->setTextAlignment(Qt::AlignCenter);
        _pParamSetWidget->item(i,3)->setTextAlignment(Qt::AlignCenter);
    }

    int nRow = 0;

    QGridLayout* playout = new QGridLayout(this);
    playout->addWidget(_pParamSetWidget,0,0,1,1);

    nRow++;

}

/**
* @brief
* @note
*
*/
void QAxisPIDWidget::onSetParamButtonClick()
{
    float lfKP=0.0,lfKI=0.0,lfKD=0.0;

    QPushButton *pushButton = dynamic_cast<QPushButton*>(this->sender());
    if(pushButton == nullptr)
    {
        return;
    }

    int x = pushButton->frameGeometry().x();
    int y = pushButton->frameGeometry().y();
    QModelIndex index = _pParamSetWidget->indexAt(QPoint(x, y));
    int row = index.row();


    int nType = pushButton->objectName().toInt();// 注入 0x5A-电流 0x45-速度 0x4A-位置


    lfKP = _pParamSetWidget->item(row,2)->text().toFloat()*100;
    lfKI = _pParamSetWidget->item(row+1,2)->text().toFloat()*100;
    lfKD = _pParamSetWidget->item(row+2,2)->text().toFloat()*100;

    emit signal_setpid(_i32AxisNo,nType,lfKP,lfKI,lfKD);

}
/**
* @brief
* @note
*
*/
void QAxisPIDWidget::showRcvdata(st_loopdata_AA loopAA)
{
    _rcvdata_AA = loopAA;
    if(_i32AxisNo == M_AXIS_AZI)
    {
        _pParamSetWidget->item(0,3)->setText(QString::number(_rcvdata_AA.azimuth_current_KP.i16data*0.001));
        _pParamSetWidget->item(1,3)->setText(QString::number(_rcvdata_AA.azimuth_current_KI.i16data*0.001));
        _pParamSetWidget->item(2,3)->setText(QString::number(_rcvdata_AA.azimuth_speed_KI.i16data*0.001));
        _pParamSetWidget->item(3,3)->setText(QString::number(_rcvdata_AA.azimuth_speed_KP.i16data*0.001));
        _pParamSetWidget->item(4,3)->setText(QString::number(_rcvdata_AA.azimuth_pos_KP.i16data*0.001));
        _pParamSetWidget->item(5,3)->setText(QString::number(_rcvdata_AA.azimuth_pos_KI.i16data*0.001));
        _pParamSetWidget->item(6,3)->setText(QString::number(_rcvdata_AA.azimuth_pos_KD.i16data*0.001));
    }
    else if(_i32AxisNo == M_AXIS_PICTH)
    {
        _pParamSetWidget->item(0,3)->setText(QString::number(_rcvdata_AA.pitch_current_KP.i16data*0.001));
        _pParamSetWidget->item(1,3)->setText(QString::number(_rcvdata_AA.pitch_current_KI.i16data*0.001));
        _pParamSetWidget->item(2,3)->setText(QString::number(_rcvdata_AA.pitch_speed_KI.i16data*0.001));
        _pParamSetWidget->item(3,3)->setText(QString::number(_rcvdata_AA.pitch_speed_KP.i16data*0.001));
        _pParamSetWidget->item(4,3)->setText(QString::number(_rcvdata_AA.pitch_pos_KP.i16data*0.001));
        _pParamSetWidget->item(5,3)->setText(QString::number(_rcvdata_AA.pitch_pos_KI.i16data*0.001));
        _pParamSetWidget->item(6,3)->setText(QString::number(_rcvdata_AA.pitch_pos_KD.i16data*0.001));
    }

}

